package com.example.listview_homework;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class GoodsAdapter extends BaseAdapter {

    GoodsData goodsData = new GoodsData();

    public GoodsAdapter(GoodsData goodsData) {
        this.goodsData = goodsData;
    }

    @Override
    public int getCount() {
        return goodsData.getTexts().length;
    }

    @Override
    public Object getItem(int position) {
        return goodsData.getTexts()[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null){
            convertView = View.inflate(parent.getContext(), R.layout.item_layout, null);
            viewHolder = new ViewHolder();
            viewHolder.textView=convertView.findViewById(R.id.tv);
            viewHolder.image = convertView.findViewById(R.id.iv);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.textView.setText(goodsData.getTexts()[position]);
        viewHolder.image.setBackgroundResource(goodsData.getImages()[position]);

        return convertView;
    }

    private class ViewHolder{
        private TextView textView;
        private ImageView image;
    }
}